/**
 * @author hhlxy
 * @version 1.0
 * @since 2025/8/5
 */
package cn.yilongquan.controller;

import cn.yilongquan.authorize.PreAuthorize;
import cn.yilongquan.service.IPermissionService;
import cn.yilongquan.system.domain.Permission;
import cn.yilongquan.system.query.PermissionQuery;
import cn.yilongquan.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "permission相关的接口")
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @ApiOperation(value = "查询指定权限信息")
    @GetMapping("/id")
    @PreAuthorize(sn = "user:findbyId",name = "根据id查询权限",descs = "根据id查询权限")
    public R findbyId(@PathVariable("id")Long id){
        return R.ok(permissionService.findById(id));
    }

    @ApiOperation(value = "查询所有权限信息")
    @GetMapping
    @PreAuthorize(sn = "user:findAll",name = "查询全部权限",descs = "查询全部权限")
    
    public R findAll(){
        return R.ok(permissionService.findAll());
    }

    @ApiOperation(value = "删除指定权限信息")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn = "user:deletebyId",name = "根据id删除权限",descs = "根据id删除权限")

    public R deletebyId(@PathVariable("id")Long id){
        permissionService.deleteById(id);
        return R.ok();
    }
    @ApiOperation(value = "删除所有权限信息")
    @PostMapping("/deleteAll")
    @PreAuthorize(sn = "user:batchDelete",name = "删除所有权限",descs = "删除所有权限")
    
    public R batchDelete(@RequestBody List<Long> ids){
        permissionService.batchDelete(ids);
        return R.ok();
    }


    //@RequestBody Permission permission：
    //将前端传入的 JSON 格式数据 自动转换为 Permission 对象（反序列化），
    // 方便后端直接操作实体类
    @ApiOperation(value = "添加或者修改指定权限信息")
    @PostMapping("/add")
    @PreAuthorize(sn = "user:add",name = "添加或修改权限",descs = "添加或修改权限")
    
    public R add(@RequestBody Permission permission){
        if(permission.getId() == null){
            permissionService.add(permission);
        }else {
            permissionService.update(permission);
        }
        return R.ok();
    }
    @ApiOperation(value = "分页查询权限信息")
    @PostMapping("/list")
    @PreAuthorize(sn = "user:pageList",name = "分页查询权限",descs = "分页查询权限信息")
    
    public R pageList(@RequestBody PermissionQuery permissionQuery){
        return R.ok(permissionService.pageList(permissionQuery));
    }
}
